
def assignValue(entry, population, value):
            if hasattr(entry, "tries"):
                population.valuate(entry, value)
            else:
                population.add(entry, new_value)


def adapt(examples, population):
    """Adapts a specimen selected from population."""
    specimen = population.select()

    old_error = examples.evaluate(specimen)
    # set old_value as evaluation of specimen

    adaption = specimen.select_adaption()
    specimen.modify(adaption)

    new_error = examples.evaluate(specimen)

    if new_error < old_error:

        assignValue(specimen, population, new_value)
        specimen.promote(adaption)

        return new_error

    else:
        specimen.undo(adaption)
        assignValue(specimen, population, old_value)
        specimen.demote(adaption)

        return old_error
